package controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.LogEnum;
import beans.RunningTask;
import dao.HistoryTaskDao;
import dao.TaskDao;
import engine.BIEngine0_1.TaskThread;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import utils.UserLogUtil;

/**
 * Servlet implementation class RestartTask
 */
public class RestartTask extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RestartTask() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取userId
		String userId = (String) request.getSession().getAttribute("userId");
		String taskHash = request.getParameter("taskHash");
		List<Object> taskHashes = new HistoryTaskDao().getSubTaskHashByBatchTaskHash(taskHash).getDatas();
		JSONArray resultArray = new JSONArray();
		if(taskHashes.size()<1){
			response.getWriter().write("error");
		}else{
			boolean isRestartSuccess = true;
			boolean isError = false;
			for(Object one:taskHashes){
				TaskThread task = new TaskDao().getTaskThread((String)one);
				RunningTask.TASKMAP.setTask((String)one,task);
				if (task == null) {
					//如果子任务中出现了任务池中不存在的任务就报错
					isError = true;
					break;
				} else {
					//如果子任务TaskThread对象恢复成功的话，就去restart，restart成功的话，返回true;restart失败的话，返回error
					isRestartSuccess = isRestartSuccess&&task.restartTask();
				}
			}
			if((!isRestartSuccess)||isError)
			{
				response.getWriter().write("error");
			}else{
				response.getWriter().write("true");
			}

			// 日志记录
			UserLogUtil.dbLog(LogEnum.historyTask, LogEnum.restartHistoryTask,
					userId, LogEnum.historyTask.getValue()+"哈希值："+taskHash);
		}
		//2018-7-22注释掉的，上面全是新加的
//		JSONObject result = new JSONObject();
//		TaskThread task = RunningTask.TASKMAP.getTask(taskHash);
//		if(task==null){
//			result.put("message", "出错了，请联系管理员");
//			result.put("result", false);
//			response.getWriter().write(result.toString());
//		}
//		else{
//			if(task.isAlive()){
//				task.pauseTask();
//				result.put("message", "重启成功");
//				result.put("result", true);
//				response.getWriter().write(result.toString());
//			}else{
//				result.put("message", "任务已完成");
//				result.put("result", false);
//				response.getWriter().write(result.toString());
//			}
//		}
	}

}
